package pack;

import java.io.FileWriter;
import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dao.EmployeeDAORemote;
import dto.EmployeeDTO;

@ManagedBean(name = "resurseBean")
@SessionScoped
public class ResurseBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	EmployeeDAORemote employeeDAO;

	private String filtru1;
	
	private String filtru2;
	
	private String filtru3;
	
	private String valoare1;
	
	private String valoare2;
	
	private String valoare3;
	
	private int id;

	private String lastname;

	private String firstname;
	
	private String username;

	private String email;
	
	private String departament;
	
	private String teamleader;
	
	private String tip_angajare;
	
	private String pozitie;
	
	List<EmployeeDTO> angajati;
	
	private String fisier;
	
	private String recrutarePrenume;
	
	private String recrutareNume;
	
	private String recrutareDepartament;
	
	private String recrutareCV;
	
	private String recrutareEmail;
	
	private Double recrutareScor;
	
	public void verifyHR(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO)fc.getExternalContext().getSessionMap().get("loggedInUser");
		
		ConfigurableNavigationHandler nav 
			= (ConfigurableNavigationHandler) 
		fc.getApplication().getNavigationHandler();
		
		if (eDTO != null){
			if (eDTO.getRank() != 2){
				nav.performNavigation("home.xhtml");
			}
		}
		else {
			nav.performNavigation("home.xhtml");
		}
		//read();
		filtru();
	}
	
	public String read(){
		angajati = employeeDAO.findAllAngajati();
		this.setAngajati(angajati);
		return "";
	}
	
	public String delFiltru(){
		angajati = employeeDAO.findAllAngajati();
		return "hrReadAngajati";
	}
	
	public String filtru(){
		read();
		String f1 = getFiltru1();
		String f2 = getFiltru2();
		String f3 = getFiltru3();
		String v1 = getValoare1();
		String v2 = getValoare2();
		String v3 = getValoare3();
		int tip = 0;
		if ("".equals(v1)){
			if ("".equals(v2)){
				if ("".equals(v3)){
					tip = 1;
					System.out.println("Tip: " + tip);
					return "";
				}
				else{
					tip = 2;
					System.out.println("Tip: " + tip);
					setAngajati(employeeDAO.getAngajatiByOne(f3, v3));
				}
			}
			else{
				if ("".equals(v3)){
					tip = 3;
					System.out.println("Tip: " + tip);
					setAngajati(employeeDAO.getAngajatiByOne(f2, v2));
				}
				else{
					tip = 4;
					System.out.println("Tip: " + tip);
					setAngajati(employeeDAO.getAngajatiByTwo(f2, v2, f3, v3));
				}
			}
		}
		else{
			if ("".equals(v2)){
				if ("".equals(v3)){
					tip = 5;
					System.out.println("Tip: " + tip);
					setAngajati(employeeDAO.getAngajatiByOne(f1, v1));
				}
				else{
					tip = 6;
					System.out.println("Tip: " + tip);
					setAngajati(employeeDAO.getAngajatiByTwo(f1, v1, f3, v3));
				}
			}
			else{
				if ("".equals(v3)){
					tip = 7;
					System.out.println("Tip: " + tip);
					setAngajati(employeeDAO.getAngajatiByTwo(f1, v1, f2, v2));
				}
				else{
					tip = 8;
					System.out.println("Tip: " + tip);
				}
			}
		}
		return "";
	}
	
	public void generateCsvFile(String s, List<EmployeeDTO> e){
		try{
			String ss = "D:\\LICENTA\\Fisiere_exportate\\" + s + ".csv";
			FileWriter writer = new FileWriter(ss);
			
			writer.append("Nr." + ","
			+ "Nume" + "," 
			+ "Prenume" + "," 
			+ "Departament" + "," 
			+ "Team leader" + ","
			+ "Tipul angajarii" + "," 
			+ "Pozitie" + "\n");
			for (int i = 0; i < e.size(); i++){
				writer.write(String.valueOf(i) + "," 
						+ e.get(i).getLastname() + ","
						+ e.get(i).getFirstname() + ","
						+ e.get(i).getDepartment() + ","
						+ e.get(i).getTeamleaderro() + ","
						+ e.get(i).getEmpl_type() + ","
						+ e.get(i).getPosition() + "\n");	
			}
			writer.flush();
			writer.close();
		}
		catch (Exception exc){
			exc.printStackTrace();
		}
	}
	
	public void resetFields(){
		setRecrutareNume("");
		setRecrutarePrenume("");
		setRecrutareEmail("");
		setRecrutareDepartament("");
		setRecrutareCV("");
		setRecrutareScor(0.0);
	}
	
	public String adauga(){
		System.out.println("Prenumele: " + getRecrutarePrenume());
		resetFields();
		return "recrutare.xhtml";
	}
	
	public String salveaza(List<EmployeeDTO> angajati){
		generateCsvFile(getFisier(), angajati);
		return "";
	}

	public String getFiltru1() {
		return filtru1;
	}

	public void setFiltru1(String filtru1) {
		this.filtru1 = filtru1;
	}

	public String getFiltru2() {
		return filtru2;
	}

	public void setFiltru2(String filtru2) {
		this.filtru2 = filtru2;
	}

	public String getFiltru3() {
		return filtru3;
	}

	public void setFiltru3(String filtru3) {
		this.filtru3 = filtru3;
	}

	public String getValoare1() {
		return valoare1;
	}

	public void setValoare1(String valoare1) {
		this.valoare1 = valoare1;
	}

	public String getValoare2() {
		return valoare2;
	}

	public void setValoare2(String valoare2) {
		this.valoare2 = valoare2;
	}

	public String getValoare3() {
		return valoare3;
	}

	public void setValoare3(String valoare3) {
		this.valoare3 = valoare3;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDepartament() {
		return departament;
	}

	public void setDepartament(String departament) {
		this.departament = departament;
	}

	public String getTeamleader() {
		return teamleader;
	}

	public void setTeamleader(String teamleader) {
		this.teamleader = teamleader;
	}

	public String getTip_angajare() {
		return tip_angajare;
	}

	public void setTip_angajare(String tip_angajare) {
		this.tip_angajare = tip_angajare;
	}

	public String getPozitie() {
		return pozitie;
	}

	public void setPozitie(String pozitie) {
		this.pozitie = pozitie;
	}

	public List<EmployeeDTO> getAngajati() {
		return angajati;
	}

	public void setAngajati(List<EmployeeDTO> angajati) {
		this.angajati = angajati;
	}

	public String getFisier() {
		return fisier;
	}

	public void setFisier(String fisier) {
		this.fisier = fisier;
	}

	public String getRecrutarePrenume() {
		return recrutarePrenume;
	}

	public void setRecrutarePrenume(String recrutarePrenume) {
		this.recrutarePrenume = recrutarePrenume;
	}

	public String getRecrutareNume() {
		return recrutareNume;
	}

	public void setRecrutareNume(String recrutareNume) {
		this.recrutareNume = recrutareNume;
	}

	public String getRecrutareDepartament() {
		return recrutareDepartament;
	}

	public void setRecrutareDepartament(String recrutareDepartament) {
		this.recrutareDepartament = recrutareDepartament;
	}

	public String getRecrutareCV() {
		return recrutareCV;
	}

	public void setRecrutareCV(String recrutareCV) {
		this.recrutareCV = recrutareCV;
	}

	public String getRecrutareEmail() {
		return recrutareEmail;
	}

	public void setRecrutareEmail(String recrutareEmail) {
		this.recrutareEmail = recrutareEmail;
	}

	public Double getRecrutareScor() {
		return recrutareScor;
	}

	public void setRecrutareScor(Double recrutareScor) {
		this.recrutareScor = recrutareScor;
	}


}
